﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Repositories;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Domains;

namespace MvcMall.Tasks
{
    public class ArticleCategoryTask : IArticleCategoryTask
    {
        private readonly IArticleCategoryRepository _articleCategoryRepository;


        public ArticleCategoryTask(IArticleCategoryRepository articleCategoryRepository)
        {
            this._articleCategoryRepository = articleCategoryRepository;
        }

        public ArticleCategory Save(ArticleCategoryCommand cmd)
        {
            if (cmd.ArticleCategoryId != null && cmd.ArticleCategoryId > 0)
            {
                // 进行更新
                var model = _articleCategoryRepository.Get(Convert.ToInt32(cmd.ArticleCategoryId));
                if (model != null)
                {
                    model.CategoryName = cmd.CategoryName;
                    return _articleCategoryRepository.Update(model);
                }

            }
            // 新增操作
            var insertModel = new ArticleCategory()
                                  {
                                      CategoryName = cmd.CategoryName
                                  };

            return _articleCategoryRepository.Save(insertModel);
        }

        public List<ArticleCategory> GetAll()
        {
            return _articleCategoryRepository.GetAll().ToList();
        }

        public ArticleCategory Get(int articleCategoryid)
        {
            return _articleCategoryRepository.Get(articleCategoryid);
        }
    }
}
